package com.shuda.business.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuda.business.crm.entity.ChannelSourceEntity;
import com.shuda.business.crm.mapper.ChannelSourceMapper;
import com.shuda.business.crm.service.ChannelSourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 渠道来源配置Service实现类
 * @author shuda
 */
@Service
@RequiredArgsConstructor
public class ChannelSourceServiceImpl extends ServiceImpl<ChannelSourceMapper, ChannelSourceEntity> 
        implements ChannelSourceService {

    private final ChannelSourceMapper channelSourceMapper;

    @Override
    public IPage<ChannelSourceEntity> getChannelSourcePage(Page<ChannelSourceEntity> page, 
                                                         String channelName, 
                                                         String channelType, 
                                                         String channelStatus, 
                                                         String ownerName) {
        LambdaQueryWrapper<ChannelSourceEntity> queryWrapper = new LambdaQueryWrapper<>();
        
        if (channelName != null && !channelName.trim().isEmpty()) {
            queryWrapper.like(ChannelSourceEntity::getChannelName, channelName);
        }
        
        if (channelType != null && !channelType.trim().isEmpty()) {
            queryWrapper.eq(ChannelSourceEntity::getChannelType, channelType);
        }
        
        if (channelStatus != null && !channelStatus.trim().isEmpty()) {
            queryWrapper.eq(ChannelSourceEntity::getChannelStatus, channelStatus);
        }
        
        if (ownerName != null && !ownerName.trim().isEmpty()) {
            queryWrapper.like(ChannelSourceEntity::getOwnerName, ownerName);
        }
        
        queryWrapper.orderByAsc(ChannelSourceEntity::getSortOrder)
                   .orderByDesc(ChannelSourceEntity::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public ChannelSourceEntity getChannelSourceById(Long id) {
        return this.getById(id);
    }

    @Override
    public ChannelSourceEntity getChannelSourceByCode(String channelCode, Long tenantId) {
        return channelSourceMapper.selectByChannelCode(channelCode, tenantId);
    }

    @Override
    public List<ChannelSourceEntity> getActiveChannelSources() {
        return channelSourceMapper.selectActiveChannels();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createChannelSource(ChannelSourceEntity channelEntity) {
        // 设置默认状态为活跃
        if (channelEntity.getChannelStatus() == null) {
            channelEntity.setChannelStatus("active");
        }
        
        // 设置默认排序号
        if (channelEntity.getSortOrder() == null) {
            Integer maxSortOrder = channelSourceMapper.selectMaxSortOrder(channelEntity.getTenantId());
            channelEntity.setSortOrder(maxSortOrder == null ? 1 : maxSortOrder + 1);
        }
        
        return this.save(channelEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateChannelSource(ChannelSourceEntity channelEntity) {
        return this.updateById(channelEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteChannelSource(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteChannelSource(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean enableChannelSource(Long id) {
        ChannelSourceEntity channelEntity = this.getById(id);
        if (channelEntity != null) {
            channelEntity.setChannelStatus("active");
            return this.updateById(channelEntity);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disableChannelSource(Long id) {
        ChannelSourceEntity channelEntity = this.getById(id);
        if (channelEntity != null) {
            channelEntity.setChannelStatus("inactive");
            return this.updateById(channelEntity);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSortOrder(Long id, Integer sortOrder) {
        ChannelSourceEntity channelEntity = this.getById(id);
        if (channelEntity != null) {
            channelEntity.setSortOrder(sortOrder);
            return this.updateById(channelEntity);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOwner(Long id, Long ownerId, String ownerName) {
        ChannelSourceEntity channelEntity = this.getById(id);
        if (channelEntity != null) {
            channelEntity.setOwnerId(ownerId);
            channelEntity.setOwnerName(ownerName);
            return this.updateById(channelEntity);
        }
        return false;
    }
}